package com.yazi.mxz.common.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.myframework.smhj.common.exception.MyEntityNotExistsException;
import com.myframework.smhj.common.exception.MyException;
import com.myframework.smhj.common.exception.MyNoPermissionException;
import com.myframework.smhj.common.tool.DateUtil;
import com.myframework.smhj.hibernate.dao.BasicService;
import com.myframework.smhj.hibernate.dao.Dao;
import com.yazi.mxz.common.db.dao.CompanyMemberDao;
import com.yazi.mxz.common.db.dao.CompanyDao;
import com.yazi.mxz.common.db.dao.MemberDao;
import com.yazi.mxz.common.db.dao.MemberRoleDao;
import com.yazi.mxz.common.db.dao.RoleDao;
import com.yazi.mxz.common.entity.ValueFactory;
import com.yazi.mxz.common.entity.beans.Company;
import com.yazi.mxz.common.entity.beans.CompanyMember;
import com.yazi.mxz.common.entity.beans.CompanyMemberId;
import com.yazi.mxz.common.entity.beans.Member;
import com.yazi.mxz.common.entity.beans.MemberRole;
import com.yazi.mxz.common.entity.beans.MemberRoleId;
import com.yazi.mxz.common.entity.beans.Role;
import com.yazi.mxz.common.service.CompanyMemberService;

@Service("companyMemberService")
public class CompanyMemberServiceImpl extends BasicService<CompanyMember> implements CompanyMemberService {
	private static Logger log = LoggerFactory.getLogger(CompanyMemberServiceImpl.class);
	
	@Resource
	CompanyMemberDao companyMemberDao;
	@Resource
	CompanyDao companyDao;
	@Resource
	MemberDao memberDao;
	@Resource
	RoleDao roleDao;
	@Resource
	MemberRoleDao memberRoleDao;
	
	@Override
	protected Dao<CompanyMember> getDao() {
		return companyMemberDao;
	}
	
	@Override
	public CompanyMember add(CompanyMember companyMember) throws MyException {
		log.debug("add：companyId={},memberId={}", 
				companyMember.getId().getCompanyId(), companyMember.getId().getMemberId());
		
		Company company = companyDao.get(companyMember.getId().getCompanyId());
		if (null == company) {
			throw new MyEntityNotExistsException();
		}
		
		// 数量检查
		if (company.getMemRemain() <= 0) {
			throw new MyNoPermissionException("成员数量受限");
		}
		company.setMemRemain(company.getMemRemain() - 1);
		companyDao.merge(company);
		
		ValueFactory.validate(companyMember);
		companyMember.setAddTime(DateUtil.getTimeStamp());
		companyMemberDao.persist(companyMember);
		
		
		Member member = memberDao.get(companyMember.getId().getMemberId());
		if (null == member) {
			throw new MyEntityNotExistsException("用户不存在");
		}
		if (member.getCompanyMembers().size() > 0) {
			throw new MyNoPermissionException("用户受限");
		}
		
		// 将用户授予企业管理员角色
		Role role = new Role();
		role.setCompany(company);
		role.setPerms("*");
		List<Role> roles = roleDao.listByExample(role);
		role = roles.get(0);
		
		MemberRole memberRole = new MemberRole();
		MemberRoleId memberRoleId = new MemberRoleId();
		memberRoleId.setMemberId(member.getMemberId());
		memberRoleId.setRoleId(role.getRoleId());
		memberRole.setId(memberRoleId);
		memberRoleDao.persist(memberRole);
		
		return companyMember;
	}

	@Override
	public void remove(long companyId, long memberId) {
		log.debug("remove: companyId={}, memberId={}", companyId, memberId);
		
		CompanyMemberId cmId = new CompanyMemberId();
		cmId.setCompanyId(companyId);
		cmId.setMemberId(memberId);
		CompanyMember cm = new CompanyMember();
		cm.setId(cmId);
		companyMemberDao.delete(cm);
		
		// 删除用户的企业管理角色
		memberRoleDao.deleteCompanyRolesForMember(memberId, companyId);
		
		// 剩余数量
		Company com = companyDao.get(companyId);
		com.setMemRemain(com.getMemRemain() + 1);
		companyDao.merge(com);
	}

}
